package xcmg.device.web.agent;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.agent.ServiceBorrowInService;
import xcmg.device.service.vo.agent.ServiceBorrowInAddVO;
import xcmg.device.service.vo.agent.ServiceBorrowInVO;
import xcmg.device.service.vo.delivery.ManualFinishVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import java.util.Arrays;
import java.util.List;

/**
 * ServiceBorrowIn Controller 实现类
 * @author huhuawei
 */
@RestController
@RequestMapping("/serviceBorrowIn")
public class ServiceBorrowInController extends BasicController {
    private static final Logger logger = LoggerFactory.getLogger(ServiceBorrowInController.class);
    @Autowired
    private ServiceBorrowInService serviceBorrowInService;

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ApiOperation(value = "借用退回单分页列表信息")
    @FastMappingInfo(needLogin = true)
    public ActionResult<PageInfo<ServiceBorrowInVO>> list(@RequestBody SearchCommonVO<ServiceBorrowInVO> condition) throws Exception {
        PageCommonVO<ServiceBorrowInVO> pageCommonVO = serviceBorrowInService.list(condition,getOrgId());
        return actionValueResult(pageCommonVO.getPageInfo());
    }


    @PostMapping(value = "/add")
    @ApiOperation(value = "借用退回单-新增修改")
    @FastMappingInfo(needLogin = true)
    public ActionResult save(@RequestBody ServiceBorrowInAddVO serviceBorrowInAddVO) throws Exception {
        if (null == serviceBorrowInAddVO) {
            return actionResult(ErrorCode.IllegalArument);
        }
        try{
            return actionResult(serviceBorrowInService.save(serviceBorrowInAddVO, getUserId(), getUserName(), getOrgId()));
        }catch (Exception e){
            return new ActionResult(ErrorCode.Failure.getCode(),e.getMessage());
        }
    }

    @RequestMapping(value = "/item", method = RequestMethod.GET)
    @ApiOperation(value = "根据id查询借用退回单详情")
    @FastMappingInfo(needLogin = true)
    public ActionResult<ServiceBorrowInAddVO> item(@RequestParam("id") String id) throws Exception {
        return actionValueResult(serviceBorrowInService.queryById(id,getOrgId()));
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiOperation(value = "删除借用退回单数据")
    @FastMappingInfo(needLogin = true)
    public ActionResult<String> delete(@RequestBody List<String> ids) {
        if (null == ids || ids.isEmpty()){
            return actionResult(ErrorCode.IllegalArument);
        }
        ErrorCode errorCode = null;
        try {
            for (String id:ids) {
                errorCode = serviceBorrowInService.delete(id,getOrgId());
            }
        } catch (Exception e) {
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
        return actionResult(errorCode);
    }


    @RequestMapping(value = "/manualFinish", method = RequestMethod.POST)
    @ApiOperation("强制完成单据")
    public ActionResult manualFinish(@RequestBody ManualFinishVO manualFinishVO) throws Exception
    {
        return actionResult(serviceBorrowInService.manualFinish(manualFinishVO.getBillNo(), manualFinishVO.getMaterialsNo(), getOrgId()));
    }

    @GetMapping(value = "/borrowingData")
    @ApiOperation(value = "根据借用单号获取新增退回数据")
    @FastMappingInfo(needLogin = true)
    public ActionResult<ServiceBorrowInAddVO> borrowingData(@RequestParam("borrowOutNo") String borrowOutNo) throws Exception {
        if (StringUtils.isBlank(borrowOutNo)){
            return actionResult(ErrorCode.IllegalArument,null);
        }
        return actionResult(ErrorCode.Success,serviceBorrowInService.borrowingData(borrowOutNo,getOrgId(),getUserId(),getUserName()));
    }

    @PostMapping(value = "/export")
    @FastMappingInfo(needLogin = true)
    @ApiOperation("根据勾选数据导出")
    public void export(String[] ids)throws Exception {
        serviceBorrowInService.export(Arrays.asList(ids));
    }

    @PostMapping(value = "/exportAll")
    @FastMappingInfo(needLogin = true)
    @ApiOperation("导出所有数据")
    public void exportAll(ServiceBorrowInVO condition)throws Exception {
        serviceBorrowInService.exportAll(condition);
    }
}
